<template>
  <div>
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="margin: 0 0 20px 0"
    >
      <el-breadcrumb-item :to="{ path: '/admin' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>校园资讯管理</el-breadcrumb-item>
      <el-breadcrumb-item>
        {{ isAdd ? "新增校园资讯" : "修改校园资讯" }}
      </el-breadcrumb-item>
    </el-breadcrumb>

    <el-card class="card_box">
      <div class="main">
        <div class="col_box">
          <div class="col_title">描述图片：</div>
          <el-upload
            class="upload-demo"
            drag
            :show-file-list="false"
            action="#"
            :http-request="imgUpload"
            :on-success="handleAvatarSuccess"
          >
            <div v-if="initData.describeImgUrl == ''">
              <i class="el-icon-upload"></i>
              <div class="el-upload__text">点击上传</div>
            </div>
            <div v-if="initData.describeImgUrl != ''">
              <img :src="initData.describeImgUrl" alt="" width="100%" />
            </div>
          </el-upload>
        </div>
        <div class="col_box">
          <div class="col_title">物品名称：</div>
          <el-input
            v-model="initData.goodsName"
            placeholder="请输入内容"
          ></el-input>
        </div>

        <div class="col_box">
          <div class="col_title">手机号码：</div>
          <el-input
            v-model="initData.telephone"
            placeholder="请输入内容"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">发布人:</div>
          <el-input
            v-model="initData.createdBy"
            placeholder="请输入内容"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title">描述信息：</div>
          <el-input
            v-model="initData.describeText"
            placeholder="请输入内容"
            type="textarea"
            :rows="8"
          ></el-input>
        </div>
        <div class="col_box">
          <div class="col_title"></div>
          <el-button type="primary" @click="clickSave">保存资料</el-button>
          <el-button @click="$router.back()">取消</el-button>
        </div>
      </div>
    </el-card>
  </div>
</template>
<script>
import {
  getByIdGoodsHelp,
  uploadImageGoodsHelp,
  getAddGoodsHelp,
  getGoodsHelpEdit,
} from "@/util/goodsHelp.js";

export default {
  data() {
    return {
      isAdd: false,

      initData: {
        id: null,
        goodsName: "",
        describeText: "",
        createdAt: "",
        createdBy: "",
        checked: 0,
        telephone: "",
        describeImg: "",
        describeImgUrl: "",
      },
    };
  },
  created() {
    // 根据Id判断是新增还是修改
    this.isAdd = this.$route.params.id == 0 ? true : false;
    // 修改 根基id获取资讯数据
    this.getByIdGoodsHelp();
  },
  mounted() {},
  methods: {
    // 根据id获取修改数据
    async getByIdGoodsHelp() {
      // 判断是新增还是修改 修改发请求;
      if (this.isAdd) {
        return;
      }
      let { data: res } = await getByIdGoodsHelp({
        id: this.$route.params.id,
      });
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.initData = res.data;
    },
    // 上传图片
    async imgUpload(e) {
      console.log(5555);

      var fm = new FormData();
      fm.append("coverFile", e.file);
      let { data: res } = await uploadImageGoodsHelp(fm);
      console.log(res);
      this.initData.describeImg = res.data.describeImg;
    },
    // 上传图片 成功的回调
    handleAvatarSuccess(res, file) {
      console.log(file);
      this.initData.describeImgUrl = URL.createObjectURL(file.raw);
    },
    // 点击上传文件
    clikcDatumName() {
      window.open(this.initData.describeImgUrl, "_blank");
    },
    // 点击按钮保存
    async clickSave() {
      console.log(this.initData);
      // 判空
      if (this.isAddData()) {
        return;
      }
      // 判断是否新增; 新增this.$route.params.id==0
      if (this.$route.params.id != 0) {
        this.initData.id = this.$route.params.id;
      } else {
        this.initData.createdUserId = window.localStorage.getItem("id");
      }
      this.initData.checked = 0;
      let { data: res } =
        this.$route.params.id == 0
          ? await getAddGoodsHelp(this.initData)
          : await getGoodsHelpEdit(this.initData);
      console.log(res);
      if (res.status != 2000) {
        return this.$message.error(res.msg);
      }
      this.$message.success(res.msg);
      this.$router.push("/admin/mutual_aid/list");
    },
    //判空
    isAddData() {
      if (
        this.initData.describeImg == "" ||
        this.initData.describeImg == null
      ) {
        this.$message.error("请输入描述图片");
        return true;
      }
      if (this.initData.goodsName == "" || this.initData.goodsName == null) {
        this.$message.error("请输入物品名称");
        return true;
      }

      if (this.initData.telephone == "" || this.initData.telephone == null) {
        this.$message.error("请输入手机号码");
        return true;
      }

      if (this.initData.createdBy == "" || this.initData.createdBy == null) {
        this.$message.error("请输入发布人");
        return true;
      }

      if (
        this.initData.describeText == "" ||
        this.initData.describeText == null
      ) {
        this.$message.error("请输入描述");
        return true;
      }
      return false;
    },
  },
};
</script>
<style lang="less" scoped>
.card_box {
  min-height: calc(100vh - 200px);
  .header_box {
    display: flex;
    margin-bottom: 24px;
  }
  .main {
    .col_box {
      display: flex;
      align-items: center;
      margin-bottom: 24px;
      .col_title {
        width: 120px;
        text-align: right;
        margin-right: 8px;
      }
      .el-input,
      .el-textarea {
        width: 560px;
      }
      /deep/ .el-upload-dragger {
        width: 340px;
        height: 170px;
      }
      .upload-demo {
        margin-bottom: 0.8rem;
      }
    }
  }
}
</style>
